/*
 * Created on Aug 1, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package IndexComponent;

import java.io.UnsupportedEncodingException;

/**
 * Description:<br>
 * &nbsp&nbsp this is used to support String keyword.
 * the keyword value saved in CharKeyword is fixed length.
 * if data need to save is large than the fixed length,it
 * throws a IllegalArgumentException,else if data length
 * small than fixed length it will fill space with ' ' to
 * the end of the data to let the length equal to the fixed
 * length.the string is ASCII mode,so one char is one byte
 * length.
 * @abstract
 * @keywords
 * @author Neil
 */
public class StringKeyword implements Keyword
{
	private static final String SPACE=" ";
	private static final String ENCODING="ASCII";
	private String value;

	public StringKeyword(String value,int fixedlength)
	{
		int i;
		StringBuffer s;
		
		if(value.length()>fixedlength)
			throw new IllegalArgumentException("keyword size too large. size: "+value.length()+" required: <="+fixedlength);
		s=new StringBuffer();
		s.append(value);
		for(i=value.length();i<fixedlength;i++) s.append(SPACE);
		this.value=s.toString();
	}
	
	public StringKeyword(byte[] key,int fixedlength)
	{
		if(key.length!=fixedlength)
			throw new IllegalArgumentException("keyword size too large. size: "+value.length()+" required: <="+fixedlength);
		try
		{
			value=new String(key,ENCODING);
		}
		catch(UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
	}

	public String getValue()
	{
		return value;
	}

	public boolean equals(Object o)
	{
		StringKeyword key;
	
		if(!(o instanceof StringKeyword)) return false;
		else
		{
			key=(StringKeyword)o;
			return value.equals(key.getValue());
		}
	}

	public int compareTo(Object o)
	{
		StringKeyword key;
	
		key=(StringKeyword)o;
		return value.compareTo(key.getValue());
	}
	
	public byte[] getBytes()
	{
		byte[] key=null;
		
		try
		{
			key=value.getBytes(ENCODING);
		}
		catch(UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		return key;
	}

	public String toString()
	{
		return value;
	}
}
